package org.wattdepot.cli;

import javax.xml.datatype.XMLGregorianCalendar;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Simple class to output fixed wattdepot data statistics.
 * 
 * @author Dean Kim
 * 
 */
public class EnergyStatistics {

  /**
   * Generate simple statistics based on fixed values.
   * 
   * @param client Valid WattDepot client
   * @return Formatted statistics.
   */
  protected String processCommand(WattDepotClient client) {

    int sampleSize = 60;
    XMLGregorianCalendar startTime;
    XMLGregorianCalendar endTime;
    XMLGregorianCalendar cursorDate;
    XMLGregorianCalendar cursorEndDate;
    double energyMax = 0;
    double energyMin = 9999999999999999999999999.00;
    double currentEnergy;
    String maxTime = " ";
    String maxEndTime = " ";
    String minTime = " ";
    String minEndTime = " ";
    StringBuffer sb = new StringBuffer(250);
    String result = " ";
    String carbonMaxTime = " ";
    String carbonMinTime = " ";
    double carbonMax = 0;
    double carbonMin = 9999999999999999999999999.00;
    double currentCarbon;
    final String mw = " MW: ";
    
    
    
    try {
      startTime = Tstamp.makeTimestamp("2009-11-01T00:00:00.000-10:00");
      endTime = Tstamp.makeTimestamp("2009-11-31T23:59:59.000-10:00");
      cursorDate = startTime;
      cursorEndDate = Tstamp.incrementHours(startTime, 1);

      while (Tstamp.lessThan(cursorEndDate, endTime)) {
        currentEnergy = client.getEnergyConsumed("SIM_OAHU_GRID", cursorDate, cursorEndDate,
            sampleSize);
        if (currentEnergy > energyMax) {
          energyMax = currentEnergy;
          maxTime = cursorDate.toString();
          maxEndTime = cursorEndDate.toString();
        }
        if (currentEnergy < energyMin) {
          energyMin = currentEnergy;
          minTime = cursorDate.toString();
          minEndTime = cursorDate.toString();
        }
        cursorDate = Tstamp.incrementHours(cursorDate, 1);
        cursorEndDate = Tstamp.incrementHours(cursorEndDate, 1);
      }
      sb.append("Max Energy Consumed Date/Time range: ");
      sb.append(maxTime);
      sb.append(' ');
      sb.append(maxEndTime);
      sb.append(' ');
      sb.append(mw);
      sb.append((energyMax / 1000000));
      sb.append("\nMin Energy Consumed Date/Time range: ");
      sb.append(minTime);
      sb.append(' ');
      sb.append(minEndTime);
      sb.append(mw);
      sb.append((energyMin / 1000000));
      
      cursorDate = Tstamp.makeTimestamp("2009-11-01T00:00:00.000-10:00");
      endTime = Tstamp.makeTimestamp("2009-11-01T23:59:59.000-10:00");
      
      for (int i = 0; i <= 30; i++) {
        currentEnergy = client.getEnergyConsumed("SIM_OAHU_GRID", cursorDate, endTime, 60);
        if (currentEnergy > energyMax) {
          energyMax = currentEnergy;
          maxTime = cursorDate.toString();
        }
        if (currentEnergy < energyMin) {
          energyMin = currentEnergy;
          minTime = cursorDate.toString();
        }
        
        currentCarbon = client.getCarbonEmitted("SIM_OAHU_GRID", cursorDate, endTime, 60);
        
        if (currentCarbon > carbonMax) {
          carbonMax = currentCarbon;
          carbonMaxTime = cursorDate.toString();
        }
        if (currentCarbon < carbonMin) {
          carbonMin = currentEnergy;
          carbonMinTime = cursorDate.toString();
        }
        
      }
      
      sb.append("\nMax Energy Consumed Day: ");
      sb.append(maxTime);
      sb.append(mw);
      sb.append((energyMax / 1000000));
      sb.append("\nMin Energy Consumed Day: ");
      sb.append(minTime);
      sb.append(mw);
      sb.append((energyMin / 1000000));
      
      sb.append("\nMax Carbon Emmitted Day: ");
      sb.append(carbonMaxTime);
      sb.append(" lbs:  ");
      sb.append(((String.format("%.5f", carbonMax))));
      sb.append("\nMin Carbon Emmited Day: ");
      sb.append(carbonMinTime);
      sb.append(" lbs:  ");
      sb.append((String.format("%.5f", carbonMin)));
      sb.append('\n');
      
      result = sb.toString();
    }
    catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }


    return result;

  }

}
